﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccess;

namespace Bussiness {
    public class TransactionBuss {
        public String registerTrans(String SID, DataTable registerList) {
            //check sessionid in database
            //return fail (-1) if not exist
           // LoginLogRepository login = new LoginLogRepository();
           // if (!login.isLogin(SID))
          //      return "-1";

            //else
            //write to db and set state is 0 
            TransactionRepository trans = new TransactionRepository();
            foreach (DataRow row in registerList.Rows) {
                int productID = int.Parse(row["ProductID"].ToString());
                int minQuanlity = int.Parse(row["MinQuantity"].ToString());
                int nextTransQuanlity = int.Parse(row["NextTransQuantity"].ToString());
                DateTime beginTransDate = DateTime.Parse(row["BeginTransDate"].ToString());
                DateTime endTransDate = DateTime.Parse(row["EndTransDate"].ToString());
                String transID = row["TransID"].ToString();

                trans.registerTrans(transID, SID, productID, beginTransDate, endTransDate, minQuanlity, nextTransQuanlity);
            }

            return "0";
        }

        /// <summary>
        /// get transactions are waiting to accept
        /// </summary>
        /// <returns></returns>
        public DataTable getWaitingToAccept() {
            TransactionRepository repository = new TransactionRepository();
            return repository.getWaitingToAccept();
        }

        public DataTable getWaitingToAccept(int partnerID) {
            TransactionRepository repository = new TransactionRepository();
            return repository.getWaitingToAccept(partnerID);
        }


        /// <summary>
        /// get partner by transaction id
        /// </summary>
        /// <param name="transID"></param>
        /// <returns></returns>
        public DataTable getPartnerByTransID(int transID) {
            TransactionRepository repository = new TransactionRepository();
            return repository.getPartnerByTransID(transID);
        }

        /// <summary>
        /// Update transaction status
        /// 0 - waitting
        /// 1 - accept successfully
        /// 2 - cancel trans
        /// 3 - accept trans error
        /// </summary>
        /// <param name="state"></param>
        public void UpdateTransactionState(int transID, int state) {
            TransactionRepository repository = new TransactionRepository();
            repository.updateTransactionState(transID, state);
        }

        /// <summary>
        /// get all pending transaction
        /// </summary>
        /// <returns></returns>
        public DataTable getPendingTransaction(int partnerID = 0) {
            TransactionRepository repository = new TransactionRepository();
            return repository.getPendingTransaction(partnerID);
        }

        /// <summary>
        /// get transaction detail
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable getDetail(int id) {
            TransactionRepository repository = new TransactionRepository();
            return repository.getDetail(id);
        }

        public DataTable getPartnerTransaction(int partnerID) {
            TransactionRepository repository = new TransactionRepository();
            return repository.getPartnerTransaction(partnerID);
        }

        public DataTable getCancelTransaction(int partnerID) {
            TransactionRepository repository = new TransactionRepository();
            return repository.getCancelTransaction(partnerID);
        }
        /// <summary>
        /// get transaction detail list
        /// </summary>
        /// <param name="transID"></param>
        /// <returns></returns>
        public DataTable detailList(int transID) {
            TransactionsDetailRepository repository = new TransactionsDetailRepository();
            return repository.detailList(transID);
        }

        public DataTable laySPChuaHopTac(int partnerID) {
            TransactionRepository de = new TransactionRepository();
            return de.laySPChuaHopTac(partnerID);
        }
    }
}
